var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bookdefault: 'testimage/timg1.jpg',//默认图片 
    activeIndex:0,
    navList: [{
      name: '书籍简介',
      _id: 'sjjj',
    },
    {
      name: '心得体会',
      _id: 'xdth', 
    },
    {
      name: '阅读点评',
      _id: 'yddp',
    },
    {
      name: '阅读排行',
      _id: 'ydph',
    }],

    startgray: '../../images/score_stars_icon_gray.png',//星级灰色
    startyellow: '../../images/score_stars_icon_yellow.png',//星级黄色
    starsy: 0,
    starsn: 5,
   

    bid:'',//书籍ID
    bookdeatil:{},//书籍详情
    xdth: [],//心得体会
    discuss: [],//阅读点评
    jfph: [],//阅读排行
    jfph1:[],
    jfph2: [],
    usid:'',
    sc:0,//是否收藏 0否 
    sj: 0,//是否加入书架 0否 

    yddpage: { //阅读点评每页展示
      page: 1,
      limit: 10,
      totalpage: 0
    },

    xdthpage: { //心得体会每页展示
      page: 1,
      limit: 10,
      totalpage: 0
    },

    jfphpage: { //阅读排行每页展示
      page: 1,
      limit:20,
      totalpage: 0
    },

    showModalStatus:false,
    minpage:'0',

    loading:false,//加入书架加载中
    disabled:false,//加入书架按钮可用设置
  },
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var usid = wx.getStorageSync('usid');
    this.setData({
      usid: usid
    })
    //获取传递的参数
    this.setData({
      bid: options.bid
    })
    this.getBookDetail();

    //心得体会（未测试）
    this.getReadlearnBybid();

    //阅读点评（未测试）
    this.getDiscussBybid();
    
    //积分排行（未测试）
    this.getReadJfPh();
  

    //查询是否收藏 是否加入书架
    this.getSfscJrsj();

  },


  getBookDetail() {
    var that=this;
    var bid=that.data.bid;
    var usid = wx.getStorageSync('usid');
    app.wxget({
      url: 'wbookdetail/getBookDetail.html',
      data: {
        bid: bid,
        usid: usid
      },
      success: function (res) {
        var bookdeatil = res.data;
        console.log(bookdeatil);
        that.setData({
          bookdeatil: bookdeatil,
          minpage: bookdeatil.MINNUM
        })
      }
    })
  },

  //获取心得体会
  getReadlearnBybid() {
    var that = this;
    var bid = that.data.bid;
    var usid = that.data.usid;
    var page = that.data.xdthpage.page;
    var limit = that.data.xdthpage.limit;
    app.wxget({
      url: 'wbookdetail/getReadlearnBybid.html',
      data: {
        bid: bid,
        page: page,
        usid: usid,
        limit: limit
      },
      success: function (res) {
        var list = res.data.xdth;
        var xdth = that.data.xdth;
        for (var i = 0; i < list.length; i++) {
          xdth.push(list[i]);
        }
        var totalpage = res.data.totalpage;
        that.setData({
          xdth: xdth,
          "xdthpage.totalpage": totalpage
        })
      }
    })
  },



  //获取阅读点评
  getDiscussBybid() {
    var that = this;
    var bid = that.data.bid;
    var page = that.data.yddpage.page;
    var limit = that.data.yddpage.limit;
    var usid = that.data.usid;
    app.wxget({
      url: 'wbookdetail/getDiscussBybid.html',
      data: {
        bid: bid,
        page: page,
        limit: limit,
        usid: usid
      },
      success: function (res) {
        var discuss = that.data.discuss;
        var dis = res.data.discuss;
        var totalpage = res.data.totalpage;
        for (var i = 0; i < dis.length; i++) {
          discuss.push(dis[i]);
        }
        that.setData({
          discuss: discuss,
          "yddpage.totalpage": totalpage
        })
      }
    })
  },


  //获取阅读排行
  getReadJfPh() {
    var that = this;
    var page = that.data.jfphpage.page;
    var limit = that.data.jfphpage.limit;
    var nj = wx.getStorageSync('nj');
    var bid = that.data.bid;
    app.wxget({
      url: 'wbookdetail/getReadJfPh.html',
      data: {
        bid: bid,
        nj: nj,
        page: page,
        limit: limit,
      },
      success: function (res) {
        var jfph = that.data.jfph;
        var jfp = res.data.jfph;
        var totalpage = res.data.totalpage;
        //前三名
        var jfph1 = that.data.jfph1;
        //除去前三名的
        var jfph2 = that.data.jfph2;
     
        for (var i = 0; i < jfp.length; i++) {
          jfph.push(jfp[i]);
        }
        for (var j = 0; j < jfph.length;j++) {
          var pm = parseInt(jfph[j].PM);
          if (pm <= 3) {
            jfph1.push(jfph[j]);
          } else {
            jfph2.push(jfph[j]);
          }
        }
        that.setData({
          jfph: jfph,
          jfph1: jfph1,
          jfph2: jfph2,
          "jfphpage.totalpage": totalpage
        })
      }
    })
  },

  //查询是否收藏  是否加入书架
  getSfscJrsj() {
    var that = this;
    var bid = that.data.bid;
    var usid = that.data.usid;
    app.wxget({
      url: 'wbookdetail/getSfscJrsj.html',
      data: {
        bid: bid,
        usid: usid
      },
      success: function (res) {
        that.setData({
          sc: res.data.SC,
          sj: res.data.SJ,
        })
      }
    })
  },

  changShow(e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    that.setData({
      activeIndex: index
    })
  },


  errorFm: function (e) {
    if (e.type == "error") {
      var bookdeatil = this.data.bookdeatil    　　　　　　　//将图片列表数据绑定到变量
      bookdeatil.FM = this.data.bookdefault //错误图片替换为默认图片
      this.setData({
        bookdeatil: bookdeatil
      })
    }
  },

  //我要收藏
  collectionbook(e) {
    var that = this;
    var bid = that.data.bid;

    var usid = wx.getStorageSync('usid');
    if (!usid){
      wx.reLaunch({
        url: '../../pages/login/login'
      })
      return;
    }
    app.wxget({
      url: 'wbookdetail/collectionbook.html',
      data: {
        bid: bid,
        usid: usid
      },
      success: function (res) {
        if (res.data){
          that.setData({
            sc: 1,
          })
          wx.showModal({
            title: '成功',
            content: '收藏成功!',
            showCancel: !1,
          })
        }else{
          wx.showModal({
            title: '友情提示',
            content: '收藏失败!',
            showCancel: !1,
          })
        }
      }
    })  
  },



  //取消收藏
  cancelcollectionbook(e) {
    var that = this;
    var bid = that.data.bid;
    var usid = wx.getStorageSync('usid');
    if (!usid) {
      wx.reLaunch({
        url: '../../pages/index/index'
      })
      return;
    }
    app.wxget({
      url: 'wbookdetail/cancelcollectionbook.html',
      data: {
        bid: bid,
        usid: usid
      },
      success: function (res) {
        if (res.data) {
          that.setData({
            sc: 0,
          })
          wx.showModal({
            title: '成功',
            content: '取消成功!',
            showCancel: !1,
          })
        } else {
          wx.showModal({
            title: '友情提示',
            content: '取消失败!',
            showCancel: !1,
          })
        }
      }
    })
  },

  powerDrawer(e) {
    var usid = wx.getStorageSync('usid');
    if (!usid) {
      wx.reLaunch({
        url: '../../pages/login/login'
      })
      return;
    }

    var minnum=this.data.bookdeatil.MINNUM;
    if(minnum==undefined ||minnum==null || minnum=="" || minnum==0){
      wx.showModal({
        title: '友情提示',
        content: '本书未设置推荐最少阅读量，请联系老师!',
        showCancel: !1,
      })
      return;
    }

    var currentStatu = e.currentTarget.dataset.statu;
    this.util(currentStatu);
  },




  util(currentStatu) {
    /* 动画部分 */
    // 第1步：创建动画实例   
    var animation = wx.createAnimation({
      duration: 200, //动画时长  
      timingFunction: "linear", //线性  
      delay: 0 //0则不延迟  
    });

    // 第2步：这个动画实例赋给当前的动画实例  
    this.animation = animation;

    // 第3步：执行第一组动画  
    animation.opacity(0).rotateX(-100).step();

    // 第4步：导出动画对象赋给数据对象储存  
    this.setData({
      animationData: animation.export()
    })

  // 第5步：设置定时器到指定时候后，执行第二组动画  
  setTimeout(function() {
      // 执行第二组动画  
      animation.opacity(1).rotateX(0).step();
      // 给数据对象储存的第一组动画，更替为执行完第二组动画的动画对象  
      this.setData({
        animationData: animation
      })
      //关闭  
      if (currentStatu == "close") {

        this.setData({
          showModalStatus: false
        });
        
        /*wx.showToast({
          title: '添加成功',
          icon: 'succes',
          duration: 1000,
          mask: true
        })*/
      }
    }.bind(this), 200)
    // 显示  
    if (currentStatu == "open") {
      this.setData({
        showModalStatus: true
      });
    }
  },



  saveplan(e) {
    var usid = wx.getStorageSync('usid');
    if (!usid) {
      wx.reLaunch({
        url: '../../pages/login/login'
      })
      return;
    }

    var that=this;

    var minpage = that.data.minpage;
    var username = wx.getStorageSync('name');
    if (minpage==""){
      wx.showModal({
        title: '友情提示',
        content: '请输入每天计划阅读页数!',
        showCancel: !1,
      })
      return;
    }
    if (parseInt(minpage) < this.data.bookdeatil.MINNUM){
      wx.showModal({
        title: '友情提示',
        content: "每天最多读书量不能低于" + this.data.bookdeatil.MINNUM+"!",
        showCancel: !1,
      })
      return;
    }
    that.setData({
      disabled: true,
      loading: true 
    })
    wx.showLoading({
      title: '保存中',
    })
    var bid = that.data.bid;
    app.wxget({
      url: 'wbookdetail/insertbookplan.html',
      data: {
        bid: bid,
        usid: usid,
        minpage: minpage,
        username: username,
      },
      success: function (res) {
        that.setData({
          disabled: false,
          loading: false 
        })
        wx.hideLoading();
        if (res.data) {
          that.util("close");
          that.setData({
            sj: 1,
          })
          wx.showToast({
            title: '添加成功',
            icon: 'succes',
            duration: 1000,
            mask: true
          })
        } else {
          wx.showModal({
            title: '友情提示',
            content: '加入书架失败!',
            showCancel: !1,
          })
        }
      }
    })
  },


  tishijrsj(e) {
    wx.showModal({
      title: '友情提示',
      content: '此书已经在你的书架中了!',
      showCancel: !1,
    })
    return;
  },

  pageinput(e) {
    this.setData({
      minpage: e.detail.value
    })
  },



  //点赞
  discussthumbsupAdd(e) {
    var that = this;
    var disid = e.currentTarget.dataset.disid;
    var index = e.currentTarget.dataset.index;
    var usid = wx.getStorageSync('usid');
    if (!usid) {
      wx.reLaunch({
        url: '../../pages/login/login'
      })
      return;
    }
    app.wxget({
      url: 'wdsj/discussthumbsupAdd.html',
      data: {
        disid: disid,
        usid: usid
      },
      success: function (res) {
        if (res.data) {
          wx.showToast({
            title: '成功',
            icon: 'success',
            duration: 2000
          })
          var discuss = that.data.discuss;
          discuss[index].DZRS = parseInt(discuss[index].DZRS) + 1;
          discuss[index].SFDZ = 1;
          that.setData({
            discuss: discuss
          })
        } else {
          wx.showToast({
            title: '失败',
            icon: 'fail',
            duration: 2000
          })
        }
      }
    })
  },

  //取消点赞
  discussthumbsupSub(e) {
    var that = this;
    var disid = e.currentTarget.dataset.disid;
    var usid = wx.getStorageSync('usid');
    if (!usid) {
      wx.reLaunch({
        url: '../../pages/login/login'
      })
      return;
    }
    var index = e.currentTarget.dataset.index;
    app.wxget({
      url: 'wdsj/discussthumbsupSub.html',
      data: {
        disid: disid,
        usid: usid
      },
      success: function (res) {
        if (res.data) {
          wx.showToast({
            title: '取消成功',
            icon: 'success',
            duration: 2000
          })
          var discuss = that.data.discuss;
          discuss[index].DZRS = parseInt(discuss[index].DZRS) - 1;
          discuss[index].SFDZ = 0;
          that.setData({
            discuss: discuss
          })
        } else {
          wx.showToast({
            title: '取消失败',
            icon: 'fail',
            duration: 2000
          })
        }
      }
    })
  },

  //预览心得体会图片
  viewimagepj: function (e) {
    var cuindex = e.target.dataset.index;
    var that = this;
    var xdth = this.data.xdth;
    var pics = xdth[cuindex].pics;
    var urls = [];
    for (var i = 0; i < pics.length; i++) {
      urls.push(pics[i].PICURL);
    }

    wx.previewImage({
      current: urls[cuindex], // 当前显示图片的http链接
      urls: urls // 需要预览的图片http链接列表
    })
  },
  
})